package com.sojoys.artifact.plugin.actor;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import com.jfinal.plugin.IPlugin;
import com.sojoys.artifact.constant.ArtifactErrorCode;
import com.sojoys.artifact.tools.ToolError;

import akka.actor.ActorSystem;
import scala.concurrent.Await;
import scala.concurrent.duration.Duration;

/**
 * @author : DengYing
 * @CreateDate : 2017年12月15日 上午11:00:20
 * @Description ：Please describe this document
 */
public class ActorPlugin implements IPlugin{

//	static final ActorManager manager = new DefaultActorManager();
	static final ActorSystem system = ActorSystem.create("LocalSystem");
	static volatile boolean start = false; 
	@Override
	public boolean start() {
		start = true;
		return true;
	}

	@Override
	public boolean stop() {
		try {
			Await.ready(system.terminate(), Duration.create(3, TimeUnit.MINUTES));
			start = false;
		} catch (TimeoutException | InterruptedException e) {
			e.printStackTrace();
		}
		return true;
	}

	public static ActorSystem getProxy(){
		if (start) {
			return system;
		}else {
			throw ToolError.newLogicError(ArtifactErrorCode.PLUGIN_NOT_INITIALIZED);
		}
	}
}
